export default {
  data() {
    return {
      timeList: [{
        name: '今日',
        checked: true,
      },
      {
        name: '昨日',
        checked: false,
      },
      {
        name: '过去7天',
        checked: false,
      }
      ],
      lineBar: [],
      chartNameList: ['浏览量', '联系', '交易'],
      scaleList: [], //我的车源左侧数量条
      carid: 0,
      sales: 0,
      Today: {}, //今天
      Yesterday: {}, //昨天
      before: {}, //过去7天
      ShowDate: {},
      showdatefalse: false,
      //chart列表显示的数据
      timeSelect: 0, //时间选择 （0今天，1昨天，2 过去七天）
      hotBrandCommonImgUrl: 'https://files.yzsheng.com/goodimg/brandimg/', //公共的品牌图片
      type: 0,
      brandName: '',
      barHeight: [], //条形图的高度
      carSourceList: [], //车子list
      pirList: [{
        carid: 0,
        name: '预检',
      }, {
        carid: 1,
        name: '上检',
      }, {
        carid: 1,
        name: '质检交车报告',
      }],
      value1: '',
      value2: '2',
      mask: true,
      options1: [{
        label: '默认排序',
        value: 1,
      },
      {
        label: '距离优先',
        value: 2,
      },
      {
        label: '价格优先',
        value: 3,
      }
      ],
      options2: [{
        label: '加热',
        value: 4,
      },
      {
        label: '极寒风暴',
        value: 5,
      }
      ],
      // 商家类型
      list: [{
        label: '不限',
        active: true,
        id: 0
      },
      {
        label: '车源商',
        active: false,
        id: 1
      },
      {
        label: '4S店',
        active: false,
        id: 2
      },
      {
        label: '汽贸',
        active: false,
        id: 3
      },
      {
        label: '直营店',
        active: false,
        id: 4
      },
      ], //（0不限，1车源商，2 4S店，3汽贸， 4直营店）
      // 排序 （0默认，1价格升序，2价格降序）
      Ascending: [{
        label: '价格升序',
        active: false,
        id: 1
      },
      {
        label: '价格降序',
        active: false,
        id: 2
      },
      ],
      listHeight: 1444, // 弹出页面高度
      borderBottom: false,
      activeColor: '#FBB000',
      ppindexlist: ['A', 'B', 'C', 'D', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'O', 'Q', 'R', 'S', 'T', 'W', 'X', 'Y',
        'Z'
      ], //品牌ABC
      addindexlist: [], //地址ABC

      // 品牌
      Brandlist: [],
      pptoView: '',
      addtoView: '',
      xuanzebrand: '',
      pzxuanzebrand: '',

      CartypeList: [], //车型配置

      Addresslist: [], // 地址
      stickyJudgment: false, //筛选置顶判断
      stickytop: 0,
      configureList: [], //配置list
      allBrands: 0, //全部品牌选中

      ConfigurationId: '',
      RegionId: '',
      StoreType: 0,
      Sorting: 0,

      searchfor: '', //搜索内容
      pageindex: 1, //当前页码
      pagecont: 0, //总条数
      topHight: 0,
      classList: [{
        'class': '品牌'
      }, {
        'class': '地址'
      }, {
        'class': '筛选'
      }],
      scrollTop: 0,

      isHeight: 0,

      tabTop: false, // tab吸顶时触发的开关

      navbarHeight: uni.getSystemInfoSync().windowTop, // 顶部高度
      menuOffsetTop: 0,
      Closefalse: false,
      zhiding: 0,
      showPage: false
    }
  },
  // 计算属性
  computed: {
    //品牌车子总条数
    brandTotal() {
      let sum = 0
      this.Brandlist.forEach(item => {
        item.data.forEach(c => {
          sum += c.carNumber
        })
      })
      return sum
    }
  },
  // 上拉加载分页
  onReachBottom() {
    this.pageindex = ++this.pageindex
    this.GetCarSourceListDataList()
    var pagezs = (this.pagecont + 10 - 1) / 10
    if (this.pageindex >= pagezs) {
      this.status = 'nomore'
      return
    }
    this.status = 'loadmore'
    setTimeout(() => {
      if (this.pageindex >= pagezs) {
        this.status = 'nomore'
      } else {
        this.status = 'loading'
      }
    }, 1000)
  },
  // vue实例加载完成
  created() {
    this.GetCarSourceListDataList() // 获取车源列表
    this.GetMyCarSourceDataDataList() //我的车源
  },
  onLoad() {
    // 获取导航栏高度
    this.navbarHeight = uni.getSystemInfoSync().windowTop
    console.log('this.navbarHeight',this.navbarHeight)

    this.$nextTick(() => {
      // 获取菜单距离顶部的距离
      uni.createSelectorQuery().select('.choose-criteria').boundingClientRect(data => {
        this.menuOffsetTop = data.top
        console.log('this.menuOffsetTop',this.menuOffsetTop)
      }).exec()
    })

    // #ifdef APP-PLUS
    this.zhiding = 90
    // #endif

    // #ifdef H5
    this.zhiding = 180
    // #endif

  },
  onPageScroll(e) {
    // 滚动监听是否吸顶
    uni.createSelectorQuery().select('.choose-criteria').boundingClientRect(data => {
      this.menuFixed = data.top <= 0 ? true : false
    }).exec()
  },
  methods: {
    // 获取车源列表
    async GetCarSourceListDataList() {
      try {
        let data = {
          'PageIndex': this.pageindex, //当前页码
          'PageSize': 10, //每页条数
          'Seek': this.searchfor, //搜索输入条件
          'Configuration_Id': this.ConfigurationId, //汽车车子配置id
          'RegionId': this.RegionId, //地区id
          'Store_Type': this.StoreType, //筛选条件店铺类型（0不限，1车源商，2 4S店，3汽贸， 4直营店）
          'Sorting': this.Sorting //排序 （0默认，1价格升序，2价格降序）
        }
        const res = await this.$api.carSource.GetCarSourceListDataList(data)
        this.pagecont = res.data.total
        this.showPage = true
        if (res.data.list === null && this.pageindex === 1) {
          this.carSourceList = []
          return false
        }
        if (this.pageindex === 1) {
          this.carSourceList = res.data.list
        } else if (this.pageindex > 1) {
          this.carSourceList = this.carSourceList.concat(res.data.list)
        }

      } catch (err) {
        console.log(err)
      }
    },
    //chart---切换选择日期
    chooseTime(index) {
      for (var i = 0; i < this.timeList.length; i++) {
        this.timeList[i].checked = false
      }
      this.timeList[index].checked = true
      if (index === 0) {
        this.timeSelect = index //时间选择
        this.GetMyCarSourceDataDataList() //我的车源
        // this.ShowDate = this.Today //  今日数据
      } else if (index === 1) {
        this.timeSelect = index //时间选择
        this.GetMyCarSourceDataDataList() //我的车源
        // this.ShowDate = this.Yesterday //  昨天数据
      } else {
        this.timeSelect = index //时间选择
        this.GetMyCarSourceDataDataList() //我的车源
        // this.ShowDate = this.before //  过去7天
      }
      this.CalculateEchart() // 计算echart Y轴坐标轴 条形图显示的长度
    },
    // 获取我的车源
    async GetMyCarSourceDataDataList() {
      try {
        let data = {
          'PageIndex': 1,
          'PageSize': 10,
          'Time': this.timeSelect, //时间选择 （0今天，1昨天，2 过去七天）
          'Store_Id': this.$store.state.userInfoDetail.store.storeId
        }
        const res = await this.$api.carSource.GetMyCarSourceDataDataList(data)
        // console.log('我的车源', res)
        this.sales = res.data.list[0].number //在售
        this.ShowDate.browse = res.data.list[0].data.browse //浏览量
        this.ShowDate.contact = res.data.list[0].data.contact //联系
        this.ShowDate.trading = res.data.list[0].data.trading //交易
        if (this.ShowDate.browse !== 0 || this.ShowDate.contact !== 0 || this.ShowDate.trading !== 0) {
          this.showdatefalse = true
          // 获取导航栏高度
          this.navbarHeight = uni.getSystemInfoSync().windowTop
          console.log('this.navbarHeight',this.navbarHeight)

          this.$nextTick(() => {
            // 获取菜单距离顶部的距离
            uni.createSelectorQuery().select('.choose-criteria').boundingClientRect(data => {
              this.menuOffsetTop = data.top
              console.log('this.menuOffsetTop',this.menuOffsetTop)
            }).exec()
          })
        } else {
          this.showdatefalse = false
        }
        this.CalculateEchart() // 计算echart Y轴坐标轴 条形图显示的长度
      } catch (err) {}
    },

    // 品牌接口请求
    async GetCarBrandList() {
      try {
        const res = await this.$api.common.GetCarBrandList()
        this.Brandlist = res.data
      } catch (err) {
        console.log(err)
      }
    },
    // 车型配置接口请求
    async GetCarModelAndConfigList(typeid) {
      try {
        let data = {
          'Bid': typeid
        }
        const res = await this.$api.common.GetCarModelAndConfigList(data)
        this.CartypeList = res.data.cartypeList
        console.log(this.CartypeList)
      } catch (err) {
        console.log(err)
      }
    },
    // // 地区接口
    async GetRegionByLevelList(typeid) {
      try {
        let data = {
          'Level': 2,
          'KeyWords': ''
        }
        const res = await this.$api.common.GetRegionByLevelList(data)
        this.Addresslist = res.data.data
        this.addindexlist = res.data.liststr
      } catch (err) {
        console.log(err)
      }
    },
    //计算品牌车子条数
    compCount(list, callback) {
      let arr = JSON.parse(JSON.stringify(list))
      arr.forEach(item => {
        item.data.forEach(b => {
          b.CartypeList.forEach(ct => {
            b.count = b.count ? (b.count + ct.CarList.length) : ct.CarList.length
          })
        })
      })
      callback(arr)
    },
    //计算echart Y轴坐标轴 条形图显示的长度
    CalculateEchart() {
      // 计算echart Y轴显示的坐标值
      //1.把浏览量 联系 交易返回的数字拼接成新的数组
      var arr = []
      for (let i in this.ShowDate) {
        arr.push(this.ShowDate[i]) //属性
      }
      //2.取出其中的最大值,乘以1.5作为echartY轴最大的坐标值
      var maxNum = Math.max.apply(null, arr)
      maxNum = maxNum * 1.5
      //3.计算Y轴的坐标值 向上取整
      this.scaleList = [Math.floor(maxNum), Math.floor(maxNum * 2 / 3), Math.floor(maxNum * 1 / 3), '0']
      //计算对应数据显示的条状图的长度 因为单位是rpx 所以最后需要再乘以2
      var browse = ((159 / Math.floor(maxNum)) * this.ShowDate.browse) * 2 + 'rpx' //浏览量的高度
      var contact = ((159 / Math.floor(maxNum)) * this.ShowDate.contact) * 2 + 'rpx' //联系的高度
      var trading = ((159 / Math.floor(maxNum)) * this.ShowDate.trading) * 2 + 'rpx' //交易的高度
      this.barHeight = [browse, contact, trading]
    },
    //选择品牌
    chooseBrand(name, id) {
      this.type = 1
      this.carid = id
      this.brandName = name
      // this.getList(1)
    },
    //切换选择全部品牌
    chooseAllBrand() {
      this.type = 0
      this.getList(0)
    },
    //搜索
    //	用户确定搜索时触发，用户按回车键，或者手机键盘右下角的"搜索"键时触发
    search(type) {
      if (this.searchfor === '') {
        setTimeout(function() {
          uni.showToast({
            title: '搜索内容不能为空',
            duration: 2000,
            icon: 'none'
          })
        }, 100)
        return false
      }
      this.searchfor = type
      this.tapscroll()
      //搜索，重新调取接口
      this.pageindex = 1
      this.GetCarSourceListDataList()
    },
    //点击搜索
    custom(type) {
      if (this.searchfor === '') {
        setTimeout(function() {
          uni.showToast({
            title: '搜索内容不能为空',
            duration: 2000,
            icon: 'none'
          })
        }, 100)
        return false
      }
      this.searchfor = type
      this.tapscroll()
      //搜索，重新调取接口
      this.pageindex = 1
      this.GetCarSourceListDataList()
    },
    clear() {
      this.searchfor = ''
      this.tapscroll()
      this.pageindex = 1
      this.GetCarSourceListDataList()
    },
    // 点击第几个
    change(index) {
      this.$u.toast(`点击了第${index}项`)
    },
    // 筛选的确定-关闭
    closeDropdown() {
      //获取筛选id，重新调取接口
      this.pageindex = 1
      this.GetCarSourceListDataList()
      this.$refs.uDropdown.close()
    },
    // 筛选的清空
    Empty() {
      for (var i = 0; i < this.list.length; i++) {
        if (this.list[i].active === true) {
          this.list[i].active = false
        }
      }
      for (var i = 0; i < this.Ascending.length; i++) {
        if (this.Ascending[i].active === true) {
          this.Ascending[i].active = false
        }
      }
      this.$refs.uDropdown.close()
      this.StoreType = 0
      this.Sorting = 0
      this.ConfigurationId = ''
      this.RegionId = ''

      this.xuanzebrand = '' //取消品牌选择
      this.pzxuanzebrand = '' //取消系列选择
      //获取筛选id，重新调取接口
      this.pageindex = 1
      this.GetCarSourceListDataList()
    },
    // 筛选选中
    tagClick(index) {
      let listbox = [] //暂存赋值
      this.list.forEach((item, i) => {
        if (i === index) {
          item.active = true //
          this.StoreType = item.id
        } else {
          item.active = false //
        }
        listbox.push(item)
      })
      this.list = listbox
    },
    // 升降序筛选选中
    AscClick(index) {
      let listbox = [] //暂存赋值
      this.Ascending.forEach((item, i) => {
        if (i === index) {
          item.active = true //
          this.Sorting = item.id
        } else {
          item.active = false //
        }
        listbox.push(item)
      })
      this.Ascending = listbox
    },
    // 品牌楼层定位
    bindToView(event) {
      var id = event.currentTarget.dataset.id
      this.pptoView = id
    },
    // 地址楼层定位
    addressToView(event) {
      var id = event.currentTarget.dataset.id
      this.addtoView = id
    },

    // 选择排序排序置顶
    tapscroll() {

      uni.pageScrollTo({
        scrollTop: this.menuOffsetTop,
        duration: 100
      })
      console.log(this.menuOffsetTop)
      // uni.createSelectorQuery().select('.topBox').boundingClientRect(data => { //目标位置的节点：类或者id
      //   uni.createSelectorQuery().select('.cheyuancss').boundingClientRect(res => { //最外层盒子的节点：类或者id
      //     var number = data.top - res.top
      //     uni.pageScrollTo({
      //       duration: 200, //过渡时间
      //       scrollTop: number, //到达距离顶部的top值
      //     })
      //   }).exec()
      // }).exec()
      this.stickyJudgment = true
    },
    //点击品牌--地址请求
    open(index) {
      this.tapscroll() //点击置顶
      this.Closefalse = true //利用关闭弹窗
      this.$refs.uDropdown.highlight() //关闭弹窗
      if (index === 0) {
        this.GetCarBrandList() //请求品牌接口
      } else if (index === 1) {
        this.GetRegionByLevelList() //请求地址接口
      }
    },
    //关闭
    close() {
      this.Closefalse = false //利用关闭弹窗
    },
    //选择品牌
    searchBrand(type) {
      if (this.xuanzebrand === type.bid) {
        this.xuanzebrand = ''
        return
      }
      this.xuanzebrand = type.bid
      this.allBrands = 0 //取消全部品牌
      this.GetCarModelAndConfigList(type.bid)
      //this.$refs.uDropdown.close() //关闭弹窗

    },
    searchBrandAll() {
      this.allBrands = 1
      this.xuanzebrand = '' //取消品牌选择
      this.pzxuanzebrand = '' //取消系列选择
      this.ConfigurationId = ''
      //选择品牌车子重新调取接口
      this.pageindex = 1
      this.GetCarSourceListDataList()

      this.Closefalse = false //利用关闭弹窗
      this.$refs.uDropdown.close()
    },
    //选择系列
    XLsearchBrand(type) {
      if (this.pzxuanzebrand === type.carTypeId) {
        this.pzxuanzebrand = ''
        return
      }
      this.pzxuanzebrand = type.carTypeId
      this.configureList = type.carList[0].list
    },
    //选择配置关闭并搜索
    Configuration(type) {
      this.ConfigurationId = type.id
      //选择品牌车子重新调取接口
      this.pageindex = 1
      this.GetCarSourceListDataList()

      this.Closefalse = false //利用关闭弹窗
      this.$refs.uDropdown.close()
    },
    //选择地址
    searchAddress(type) {
      this.RegionId = type.id
      //获取地区ID，重新调取接口
      this.pageindex = 1
      this.GetCarSourceListDataList()

      this.Closefalse = false //利用关闭弹窗
      this.$refs.uDropdown.close() //关闭弹窗
    },
    searchAddressAll() {
      this.RegionId = ''
      //获取地区ID，重新调取接口
      this.pageindex = 1
      this.GetCarSourceListDataList()
      this.$refs.uDropdown.close() //关闭弹窗
    },
    //组件吸顶时触发
    fixed() {
      this.stickyJudgment = true
    },
    //组件取消吸顶时触发--冒泡了！！
    unfixed() {
      this.stickyJudgment = false
      this.$refs.uDropdown.close() //关闭弹框
    },

    // 管理车源
    gotoSxy() {
      uni.navigateTo({
        url: '/pages_carSource/managementOptions/managementOptions'
      })
    },
    // 跳转页面
    fabuxcclick(url) {
      uni.navigateTo({
        url: url
      })
    },
    //车源详情
    Carsourcedetails(typeid) {
      uni.navigateTo({
        url: '/pages_carSource/productDetail/productDetail?goodsid=' + typeid
      })
    },
  }
}
